import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';

class LoadSvgWidget extends StatelessWidget {
  final String svgPath;
  final double width;
  final double height;
  final double radius;
  final BoxFit boxFit;
  final bool needBorder;
  final Color borderColor;
  final double borderWidth;
  final Color? color;
  final BlendMode? colorBlendMode;

  const LoadSvgWidget(
      this.svgPath, {
        super.key,
        this.width = 300,
        this.height = 300,
        this.radius = 0.0,
        this.boxFit = BoxFit.cover,
        this.needBorder = false,
        this.borderColor = Colors.white,
        this.borderWidth = 2,
        this.color,
        this.colorBlendMode,
      });

  @override
  Widget build(BuildContext context) {
    Widget svgWidget;

    if (svgPath.contains('http')) {
      // 网络SVG
      svgWidget = SvgPicture.network(
        svgPath,
        width: width,
        height: height,
        fit: boxFit,
        colorFilter: color != null
            ? ColorFilter.mode(color!, colorBlendMode ?? BlendMode.srcIn)
            : null,
        placeholderBuilder: _buildPlaceholder,
      );
    } else if (svgPath.contains('assets')) {
      // 资源文件SVG
      svgWidget = SvgPicture.asset(
        svgPath,
        width: width,
        height: height,
        fit: boxFit,
        colorFilter: color != null
            ? ColorFilter.mode(color!, colorBlendMode ?? BlendMode.srcIn)
            : null,
        placeholderBuilder: _buildPlaceholder,
      );
    } else {
      // 本地文件SVG
      svgWidget = SvgPicture.file(
        File(svgPath),
        width: width,
        height: height,
        fit: boxFit,
        colorFilter: color != null
            ? ColorFilter.mode(color!, colorBlendMode ?? BlendMode.srcIn)
            : null,
        placeholderBuilder: _buildPlaceholder,
      );
    }

    // 添加边框和圆角
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(radius),
        border: needBorder
            ? Border.all(
          color: borderColor,
          width: borderWidth.w,
          strokeAlign: BorderSide.strokeAlignOutside,
        )
            : null,
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius),
        child: svgWidget,
      ),
    );
  }

  Widget _buildPlaceholder(BuildContext context) {
    return Container(
      width: width,
      height: height,
      decoration: BoxDecoration(
        color: Colors.grey[200],
        borderRadius: BorderRadius.circular(radius),
      ),
      child: Center(
        child: Icon(
          Icons.image,
          size: width * 0.3,
          color: Colors.grey[400],
        ),
      ),
    );
  }
}